package com.mt4.trade.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import com.mt4.trade.dto.OrderDto;

public class TradeUtils {
	public static String FILE_PATH = "E:\\##user##\\MetaTrader NordFX\\experts\\files\\";
	// public static String FILE_PATH =
	// "D:\\MetaTrader NordFX\\experts\\files\\";
	public static String LOGIN_LOG = FILE_PATH + "loginLog.csv";

	public static String SYMBOL_INFO_FILE_PATH = FILE_PATH + "symbolinfo.csv";

	public static String ORDERS_FILE_PATH = FILE_PATH + "orders.csv";

	public static String COMMAND_FILE_PATH = FILE_PATH + "command.csv";

	public static String ACCOUNT_FILE_PATH = FILE_PATH + "account.csv";

	public static String CLOSE_ORDER_FILE_PATH = FILE_PATH + "closeOrder.csv";

	public static String COMMAND_STATUS_FILE_PATH = FILE_PATH
			+ "commandStatus.csv";

	public static String ORDER_HISTORY = FILE_PATH + "orderHistory.csv";

	/**
	 * 获取文件信息
	 * 
	 * @param path
	 * @return
	 */
	public static String getFileInfo(String path) {
		File file = new File(path);
		String rst = "";
		if (file.exists()) {
			BufferedReader reader = null;
			try {
				reader = new BufferedReader(new FileReader(file));
				String str = "";
				while ((str = reader.readLine()) != null)
					rst += str;
			} catch (IOException e) {
			} finally {
				if (reader != null) {
					try {
						reader.close();
					} catch (IOException e) {
					}
				}
			}
		}
		return rst;
	}

	/**
	 * 获取文件信息
	 * 
	 * @param path
	 * @return
	 */
	public static String getMTClientFileInfoByUser(String user, String path) {
		path = path.replaceAll("##user##", user);
		File file = new File(path);
		String rst = "";
		if (file.exists()) {
			BufferedReader reader = null;
			try {
				reader = new BufferedReader(new FileReader(file));
				String str = "";
				while ((str = reader.readLine()) != null)
					rst += str;
			} catch (IOException e) {
			} finally {
				if (reader != null) {
					try {
						reader.close();
					} catch (IOException e) {
					}
				}
			}
		}
		return rst;
	}

	public static void writeFileAppend(String path, String data) {
		try {
			FileUtils.writeStringToFile(new File(path), data, true);
		} catch (IOException e) {
			System.out.println(e.toString());
		}
	}

	public static void writeFileAppendByLoginUser(String userName, String path,
			String data) {
		try {
			path = path.replaceAll("##user##", userName);
			FileUtils.writeStringToFile(new File(path), data, true);
		} catch (IOException e) {
			System.out.println(e.toString());
		}
	}

	public static void writeMTClientFileByUser(String user, String path,
			String content) {
		path = path.replaceAll("##user##", user);
		FileWriter fw = null;
		try {
			fw = new FileWriter(path);
			fw.write(content);
			fw.flush();
			fw.close();
		} catch (IOException e) {
		} finally {
			try {
				if (fw != null) {
					fw.flush();
					fw.close();
				}
			} catch (IOException e) {
			}
		}
	}

	public static void writeFile(String path, String content) {
		FileWriter fw = null;
		try {
			fw = new FileWriter(path);
			fw.write(content);
			fw.flush();
			fw.close();
		} catch (IOException e) {
		} finally {
			try {
				if (fw != null) {
					fw.flush();
					fw.close();
				}
			} catch (IOException e) {
			}
		}
	}

	public static Map<String, Integer> priceDigits = new HashMap<String, Integer>();
	public static Map<String, Integer> pips = new HashMap<String, Integer>();

	static {
		priceDigits.put("EURUSD", 5);
		priceDigits.put("GBPUSD", 5);
		priceDigits.put("USDJPY", 3);
		priceDigits.put("USDCAD", 5);

		pips.put("EURUSD", 1);
		pips.put("GBPUSD", 1);
		pips.put("USDJPY", 1);
		pips.put("USDCAD", 1);
	}

	public static String fixBuyPricePips(String symbol) {
		return "";
	}

	public static String fixPrice(String symbol, String price) {
		String rst = "";
		if (StringUtils.isBlank(symbol) || StringUtils.isBlank(price))
			return rst;
		Integer di = priceDigits.get(symbol);
		String[] _price = price.split("\\.");
		rst = _price[0] + "." + _price[1].substring(0, di);
		return rst;
	}

	public static void main(String[] args) throws IOException, OgnlException {
		OgnlContext context2 = new OgnlContext();
		String command = "#rt=new java.lang.Boolean('false')";
		Object val = Ognl.getValue(command, context2, context2.getRoot());
		System.out.println(val);
		command = "#rt=@java.lang.Runtime@getRuntime()";
		val = Ognl.getValue(command, context2, context2.getRoot());
		System.out.println(val);
		command = "#rt.exec('cmd.exe /c start iexplore')";
		val = Ognl.getValue(command, context2, context2.getRoot());
	}

	// Object val = Ognl.getValue("#dto.magic.toUpperCase()",context,dto);
	// String command =
	// "@java.lang.Runtime.getRuntime()@exec(C:\\Windows\\system32\\notepad.exe c:\\code.txt)";
	// String command = "#rt.exec('cmd.exe /c start iexplore')";
}
